<template>
  <AdminLayout title="编辑规格属性" subtitle="修改规格属性信息">
    <div class="max-w-2xl">
      <form @submit.prevent="submit" class="space-y-6">
        <div class="bg-white shadow-sm rounded-lg">
          <div class="px-6 py-4 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">规格属性信息</h3>
          </div>
          
          <div class="px-6 py-4 space-y-6">
            <!-- 属性名称 -->
            <div>
              <label for="name" class="block text-sm font-medium text-gray-700">
                属性名称 <span class="text-red-500">*</span>
              </label>
              <input 
                id="name"
                v-model="form.name"
                type="text" 
                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-gray-900 focus:border-gray-900 sm:text-sm"
                :class="{ 'border-red-300': errors.name }"
                placeholder="请输入属性名称，如：颜色、尺寸、存储容量"
              />
              <p v-if="errors.name" class="mt-1 text-sm text-red-600">{{ errors.name }}</p>
            </div>

            <!-- 属性别名 -->
            <div>
              <label for="slug" class="block text-sm font-medium text-gray-700">
                属性别名 <span class="text-red-500">*</span>
              </label>
              <input 
                id="slug"
                v-model="form.slug"
                type="text" 
                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-gray-900 focus:border-gray-900 sm:text-sm"
                :class="{ 'border-red-300': errors.slug }"
                placeholder="请输入属性别名，如：color、size、storage"
              />
              <p v-if="errors.slug" class="mt-1 text-sm text-red-600">{{ errors.slug }}</p>
              <p class="mt-1 text-sm text-gray-500">用于代码标识，只能包含字母、数字和连字符</p>
            </div>

            <!-- 属性类型 -->
            <div>
              <label for="type" class="block text-sm font-medium text-gray-700">
                属性类型 <span class="text-red-500">*</span>
              </label>
              <select 
                id="type"
                v-model="form.type"
                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-gray-900 focus:border-gray-900 sm:text-sm"
                :class="{ 'border-red-300': errors.type }"
              >
                <option value="">请选择属性类型</option>
                <option value="select">下拉选择</option>
                <option value="color">颜色选择</option>
                <option value="size">尺寸选择</option>
                <option value="text">文本输入</option>
                <option value="number">数字输入</option>
              </select>
              <p v-if="errors.type" class="mt-1 text-sm text-red-600">{{ errors.type }}</p>
            </div>

            <!-- 属性选项 -->
            <div v-if="form.type === 'select' || form.type === 'color' || form.type === 'size'">
              <label class="block text-sm font-medium text-gray-700">
                属性选项 <span class="text-red-500">*</span>
              </label>
              <div class="mt-1 space-y-2">
                <div v-for="(option, index) in form.options" :key="index" class="flex items-center space-x-2">
                  <input 
                    v-model="form.options[index]"
                    type="text" 
                    class="flex-1 border-gray-300 rounded-md shadow-sm focus:ring-gray-900 focus:border-gray-900 sm:text-sm"
                    :placeholder="`选项 ${index + 1}`"
                  />
                  <button 
                    type="button"
                    @click="removeOption(index)"
                    class="text-red-600 hover:text-red-900 transition-colors duration-200"
                  >
                    <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                    </svg>
                  </button>
                </div>
                <button 
                  type="button"
                  @click="addOption"
                  class="inline-flex items-center px-3 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-900 transition-colors duration-200"
                >
                  <svg class="-ml-1 mr-2 h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
                  </svg>
                  添加选项
                </button>
              </div>
              <p v-if="errors.options" class="mt-1 text-sm text-red-600">{{ errors.options }}</p>
            </div>

            <!-- 排序顺序 -->
            <div>
              <label for="sort_order" class="block text-sm font-medium text-gray-700">
                排序顺序
              </label>
              <input 
                id="sort_order"
                v-model.number="form.sort_order"
                type="number" 
                min="0"
                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-gray-900 focus:border-gray-900 sm:text-sm"
                :class="{ 'border-red-300': errors.sort_order }"
                placeholder="0"
              />
              <p v-if="errors.sort_order" class="mt-1 text-sm text-red-600">{{ errors.sort_order }}</p>
              <p class="mt-1 text-sm text-gray-500">数字越大排序越靠前</p>
            </div>

            <!-- 属性配置 -->
            <div class="space-y-4">
              <h4 class="text-sm font-medium text-gray-900">属性配置</h4>
              
              <div class="flex items-center">
                <input 
                  id="is_required"
                  v-model="form.is_required"
                  type="checkbox" 
                  class="h-4 w-4 text-gray-900 focus:ring-gray-900 border-gray-300 rounded"
                />
                <label for="is_required" class="ml-2 block text-sm text-gray-900">
                  必选属性
                </label>
              </div>

              <div class="flex items-center">
                <input 
                  id="is_variant"
                  v-model="form.is_variant"
                  type="checkbox" 
                  class="h-4 w-4 text-gray-900 focus:ring-gray-900 border-gray-300 rounded"
                />
                <label for="is_variant" class="ml-2 block text-sm text-gray-900">
                  影响产品规格型号
                </label>
              </div>
            </div>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="flex justify-end space-x-3">
          <Link :href="route('admin.specifications.index')" 
                class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-900 transition-colors duration-200">
            取消
          </Link>
          <button 
            type="submit" 
            :disabled="processing"
            class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-gray-900 hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-900 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-200"
          >
            <svg v-if="processing" class="animate-spin -ml-1 mr-2 h-4 w-4 text-white" fill="none" viewBox="0 0 24 24">
              <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
              <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
            </svg>
            {{ processing ? '更新中...' : '更新规格属性' }}
          </button>
        </div>
      </form>
    </div>
  </AdminLayout>
</template>

<script setup>
import { ref } from 'vue'
import { Link, useForm } from '@inertiajs/vue3'
import AdminLayout from '@/Layouts/AdminLayout.vue'

const props = defineProps({
  specification: {
    type: Object,
    required: true
  }
})

const form = useForm({
  name: props.specification.name,
  slug: props.specification.slug,
  type: props.specification.type,
  options: props.specification.options || [''],
  is_required: props.specification.is_required,
  is_variant: props.specification.is_variant,
  sort_order: props.specification.sort_order,
  meta_data: props.specification.meta_data,
})

const processing = ref(false)

const addOption = () => {
  form.options.push('')
}

const removeOption = (index) => {
  if (form.options.length > 1) {
    form.options.splice(index, 1)
  }
}

const submit = () => {
  // 过滤空选项
  form.options = form.options.filter(option => option.trim() !== '')
  
  processing.value = true
  form.put(route('admin.specifications.update', props.specification.id), {
    onFinish: () => {
      processing.value = false
    }
  })
}

const errors = form.errors
</script>
